package com.example.onlinemonitoring.serviceImpl;

import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.ConditionDispose;
import com.example.onlinemonitoring.Util.DefaultProperty;
import com.example.onlinemonitoring.dao.DlqjchistoryMapper;
import com.example.onlinemonitoring.dao.DlqjcsensorMapper;
import com.example.onlinemonitoring.dao.DlqjcsensorTestMapper;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Dlqjcsensor;
import com.example.onlinemonitoring.model.Dlqjchistory;
import com.example.onlinemonitoring.model.DlqjcsensorTest;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.alarmmessage;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ch.qos.logback.core.joran.util.beans.BeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.service.DlqjcsensorService;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.Util.HexUtil;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @description Dlqjcsensor接口实现
 * @author generator
 * @since 2022-04-09 13:03:09
 */
@Service("dlqjcServiceImpl")
public class DlqjcsensorServiceImpl extends SensorAbstractBaseService<Dlqjcsensor, Dlqjchistory>
        implements DlqjcsensorService {

    @Autowired
    private DlqjchistoryMapper dlqjchistoryMapper;
    @Autowired
    private DlqjcsensorMapper dlqjcsensorMapper;
    @Autowired
    private DlqjcsensorTestMapper dlqjcsensorTestMapper;

    public MyResult<Float[][]> selectdlqcurve(Dlqjcsensor record) {
        if (record.getCmd() == null) {
            List<Float[][]> list = new ArrayList<>();
            List<Dlqjcsensor> dlq = dlqjcsensorMapper.selectbytotal(record);
            Iterator<Dlqjcsensor> iter = dlq.iterator();
            int ABC = 64;
            Float curr = 0f;
            Dlqjcsensor record1 = new Dlqjcsensor();
            Example example = new Example(Dlqjcsensor.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid", record.getStationid());
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
            criteria.andEqualTo("cmd", 67);
            List<Dlqjcsensor> dlqlist = selectByExample(example);
            if (dlqlist.size() != 0) {
                record1 = dlqlist.get(0);
            } else {
                MyResult<Float[][]> result = new MyResult<Float[][]>();
                result.setCode(1);
                result.setMsg("数据为空");
                return result;
            }
            if (record1.getLastastate() != record.getLaststate()) {
                MyResult<Float[][]> result = new MyResult<Float[][]>();
                result.setCode(1);
                result.setMsg("数据为空");
                return result;
            }
            while (iter.hasNext()) {
                Dlqjcsensor dlqjc = iter.next();
                switch (ABC) {
                    case 64:
                        curr = Math.abs(CommPortManager.isnull(dlqjc.getLastaccurrent())
                                - CommPortManager.isnull(dlqjc.getLastaacurrent()));
                        break;
                    case 65:
                        curr = Math.abs(CommPortManager.isnull(dlqjc.getLastbccurrent())
                                - CommPortManager.isnull(dlqjc.getLastbacurrent()));
                        break;
                    case 66:
                        curr = Math.abs(CommPortManager.isnull(dlqjc.getLastcccurrent())
                                - CommPortManager.isnull(dlqjc.getLastcacurrent()));
                        break;
                    default:
                        break;
                }
                Float f[][] = new Float[2000][2];
                for (int i = 0; i < 2000; i++) {
                    f[i][0] = i / 10f;
                    if (record.getLaststate() == 1) {
                        // 主分
                        if (i / 10 < CommPortManager.isnull(dlqjc.getLastt5time())) {
                            f[i][1] = 0f;
                        } else {
                            f[i][1] = (float) Math.sin(1.5 * i / 50f) * curr;
                        }
                    } else if (record.getLaststate() == 2) {
                        // 合闸
                        if (i / 10 > CommPortManager.isnull(dlqjc.getLastt5time())) {
                            f[i][1] = 0f;
                        } else {
                            f[i][1] = (float) Math.sin(1.5 * i / 50f) * curr;
                        }

                    }
                }
                list.add(f);
                ABC++;
            }
            MyResult<Float[][]> result = new MyResult<Float[][]>();
            if (list.size() != 0) {
                result.setCode(0);
                result.setMsg("success");
                result.setCount(list.size());
                result.setData(list);
            } else {
                result.setCode(1);
                result.setMsg("数据为空");
            }
            return result;
        } else {
            List<Float[][]> list = new ArrayList<>();
            Dlqjcsensor dlq = dlqjcsensorMapper.selectsinglebytotal(record);
            Float c[][] = new Float[6][2];
            c[0][0] = (float) CommPortManager.isnull(dlq.getLastt0time());
            c[1][0] = (float) CommPortManager.isnull(dlq.getLastt1time());
            c[2][0] = (float) CommPortManager.isnull(dlq.getLastt2time());
            c[3][0] = (float) CommPortManager.isnull(dlq.getLastt3time());
            c[4][0] = (float) CommPortManager.isnull(dlq.getLastt4time());
            c[5][0] = (float) CommPortManager.isnull(dlq.getLastt5time());

            c[0][1] = (float) CommPortManager.isnull(dlq.getLasti0current());
            c[1][1] = (float) CommPortManager.isnull(dlq.getLasti1current());
            c[2][1] = (float) CommPortManager.isnull(dlq.getLasti2current());
            c[3][1] = (float) CommPortManager.isnull(dlq.getLasti3current());
            c[4][1] = (float) CommPortManager.isnull(dlq.getLasti4current());
            c[5][1] = (float) CommPortManager.isnull(dlq.getLasti5current());
            list.add(c);
            MyResult<Float[][]> result = new MyResult<Float[][]>();
            if (list.size() != 0) {
                result.setCode(0);
                result.setMsg("success");
                result.setCount(list.size());
                result.setData(list);
            } else {
                result.setCode(1);
                result.setMsg("数据为空");
            }
            return result;
        }
    }

    public MyResult<Integer> selectChangedWave(Dlqjcsensor record) {
        List<Dlqjcsensor> dlqlist = new ArrayList<>();
        dlqlist = dlqjcsensorMapper.selectbytotal(record);
        List<Integer> list = new ArrayList<>();
        int size = dlqlist.size();
        for (int i = 0; i < size; i++) {
            if (dlqlist.get(i).getLastt5time() == null) {
                list.add(0);
            } else {
                list.add(dlqlist.get(i).getLastt5time());
            }
        }
        MyResult<Integer> result = new MyResult<Integer>();
        if (list.size() != 0) {
            list.add(HexUtil.MAX(new int[] { Math.abs(list.get(0) - list.get(1)), Math.abs(list.get(0) - list.get(2)),
                    Math.abs(list.get(1) - list.get(2)) }));
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list.size());
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }

    public MyResult<Float[][]> selectdlqhiscurve(Dlqjchistory dlqhis) {
        List<Float[][]> list = new ArrayList<>();
        dlqhis = dlqjchistoryMapper.selectByPrimaryKey(dlqhis);
        MyResult<Float[][]> result = new MyResult<Float[][]>();
        if (dlqhis != null) {
            Float c[][] = new Float[6][2];
            c[0][0] = (float) HexUtil.isnull(dlqhis.getT0time());
            c[1][0] = (float) HexUtil.isnull(dlqhis.getT1time());
            c[2][0] = (float) HexUtil.isnull(dlqhis.getT2time());
            c[3][0] = (float) HexUtil.isnull(dlqhis.getT3time());
            c[4][0] = (float) HexUtil.isnull(dlqhis.getT4time());
            c[5][0] = (float) HexUtil.isnull(dlqhis.getT5time());

            c[0][1] = (Float) dlqhis.getI0current();
            c[1][1] = (Float) dlqhis.getI1current();
            c[2][1] = (Float) dlqhis.getI2current();
            c[3][1] = (Float) dlqhis.getI3current();
            c[4][1] = (Float) dlqhis.getI4current();
            c[5][1] = (Float) dlqhis.getI5current();
            list.add(c);
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list.size());
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }

    public MyResult<Dlqjchistory> selectdlqhisgroupbystate(Dlqjchistory record, String beginTime, String endTime) {
        List<Dlqjchistory> list = new ArrayList<>();
        list = dlqjchistoryMapper.selectgroupbystate(record, beginTime, endTime);
        MyResult<Dlqjchistory> result = new MyResult<Dlqjchistory>();
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list.size());
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }

    public int updatedlqtest(DlqjcsensorTest record) {
        Example example = new Example(DlqjcsensorTest.class);
        Example.Criteria criteria = example.createCriteria();
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getCmd() != null) {
            criteria.andEqualTo("cmd", record.getCmd());
        }
        if (record.getLaststate() != null) {
            criteria.andEqualTo("laststate", record.getLaststate());
        }
        return dlqjcsensorTestMapper.updateByExampleSelective(record, example);
    }

    /**
     * 查询分页实时表
     * 
     * @param record
     * @param qo
     * @return
     */
    @Override
    public MyResult<Dlqjcsensor> getList(Dlqjcsensor record, QueryObject qo) {
        if (qo.getPage() != null || qo.getLimit() != null) {
            PageHelper.startPage(qo.getPage(), qo.getLimit());
        }
        Example example = new Example(Dlqjcsensor.class);
        Example.Criteria criteria = example.createCriteria();
        if (record.getStationid() != null) {
            criteria.andEqualTo("stationid", record.getStationid());
        }
        if (record.getDevicegroup() != null) {
            criteria.andEqualTo("devicegroup", record.getDevicegroup());
        }
        if (record.getDeviceaddr() != null) {
            criteria.andEqualTo("deviceaddr", record.getDeviceaddr());
        }
        if (record.getInuse() != null) {
            criteria.andEqualTo("inuse", record.getInuse());
        }
        if (record.getCmd() != null) {
            criteria.andEqualTo("cmd", record.getCmd());
        }
        if (record.getLastalarmlevel() != null) {
            criteria.andEqualTo("lastalarmlevel", record.getLastalarmlevel());
        }
        if (record.getLastcommunicationstatus() != null) {
            criteria.andEqualTo("lastcommunicationstatus", record.getLastcommunicationstatus());
        }
        List<Dlqjcsensor> list = selectByExample(example);
        MyResult<Dlqjcsensor> result = new MyResult<Dlqjcsensor>();
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            if (qo.getPage() != null || qo.getLimit() != null) {
                PageInfo<Dlqjcsensor> pi = new PageInfo<>(list);
                result.setCount((int) pi.getTotal());
            } else {
                result.setCount(list.size());
            }
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }

    public MyResult<Float[][]> selectdlqhistorydatacurve(Dlqjchistory record, pagebase pagebase) {
        List<Dlqjchistory> list = new ArrayList<>();
        List<Float[][]> arrlist = new ArrayList<>();

        list = dlqjchistoryMapper.selecthistorydatacurve(record, pagebase.getBegintime(), pagebase.getEndtime());
        MyResult<Float[][]> result = new MyResult<Float[][]>();
        int count = list.size();
        if (count > 1000) {
            int c = (int) Math.floor(count / 1000f);
            Float arr[][] = new Float[1000][2];
            for (int j = 0; j < 1000; j++) {
                arr[j][0] = (float) list.get(j * c).getDatatime().getTime();
                arr[j][1] = list.get(j * c).getLifetime();
            }
            arrlist.add(arr);
            result.setCode(0);
            result.setMsg("success");
            result.setCount(1000);
            result.setData(arrlist);
        } else {
            Float arr[][] = new Float[count][2];
            for (int i = 0; i < count; i++) {
                arr[i][0] = (float) list.get(i).getDatatime().getTime();
                arr[i][1] = list.get(i).getLifetime();
            }
            arrlist.add(arr);
            if (list.size() != 0) {
                result.setCode(0);
                result.setMsg("success");
                result.setCount(count);
                result.setData(arrlist);
            } else {
                result.setCode(1);
                result.setMsg("数据为空");
            }
        }
        return result;
    }

    @Override
    public int Add(Appdevice record) {
        Dlqjcsensor dlqjc = new Dlqjcsensor();
        DlqjcsensorTest dlqjc_test = new DlqjcsensorTest();
        dlqjc.setStationid(record.getStationid());
        dlqjc.setDevicegroup(record.getDevicegroup());
        for (int i = record.getAddrmin(); i < record.getAddrmax() + 1; i++) {
            // 断路器实时表插入

            dlqjc.setStationid(record.getStationid());
            dlqjc.setDeviceaddr(i);
            dlqjc.setDevicegroup(record.getDevicegroup());
            if (dlqjcsensorMapper.selectbytotal(dlqjc).size() == 0) {
                dlqjc.setPointname("新监测点");
                dlqjc.setCmd(67);
                dlqjc.setLastdatatime(new Date());
                dlqjc.setLaststate(-1);
                dlqjc.setLaststoragestate(-1);
                dlqjc.setLastbstate(-1);
                dlqjc.setLastbstoragestate(-1);
                dlqjc.setLastcstate(-1);
                dlqjc.setLastcstoragestate(-1);
                insertSelective(dlqjc);
                for (int j = 1; j <= 4; j++) {
                    for (int k = 0x40; k <= 0x42; k++) {
                        dlqjc.setLaststate(j);
                        dlqjc.setCmd(k);
                        insertSelective(dlqjc);
                    }
                }
            }
            dlqjc_test.setStationid(record.getStationid());
            dlqjc_test.setDeviceaddr(i);
            dlqjc_test.setDevicegroup(record.getDevicegroup());
            if (dlqjcsensorTestMapper.selectbytotal(dlqjc_test).size() == 0) {
                dlqjc_test.setPointname("断路器监测");
                for (int j = 1; j <= 4; j++) {
                    // 断路器测试表插入
                    dlqjc_test.setLaststate(j);
                    dlqjcsensorTestMapper.insertSelective(dlqjc_test);
                }
            }
        }
        return 1;
    }

    /**
     * 获取所有在运行的设备总条数
     * 
     * @param stationid
     * @param devicegroup
     * @return
     */
    @Override
    public int getTotalCount(int stationid, int devicegroup) {
        Example example = new Example(Dlqjcsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        criteria.andEqualTo("cmd", 67);
        criteria.andEqualTo("inuse", 1);
        return selectCountByExample(example);
    }

    /**
     * 获取报警中的设备总条数
     * 
     * @param stationid
     * @param devicegroup
     * @return
     */
    @Override
    public int getAlarmCount(int stationid, int devicegroup) {
        Example example = new Example(Dlqjcsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        criteria.andEqualTo("inuse", 1);
        criteria.andEqualTo("cmd", 67);
        criteria.andEqualTo("lastalarmlevel", 2);
        return selectCountByExample(example);
    }

    /**
     * 获取预警中的设备总条数
     * 
     * @param stationid
     * @param devicegroup
     * @return
     */
    @Override
    public int getWarnCount(int stationid, int devicegroup) {
        Example example = new Example(Dlqjcsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        criteria.andEqualTo("inuse", 1);
        criteria.andEqualTo("lastalarmlevel", 1);
        return selectCountByExample(example);
    }

    /**
     * 获取通讯正常的（不判断报警不报警）设备总条数
     * 
     * @param stationid
     * @param devicegroup
     * @return
     */
    @Override
    public int getNormalCount(int stationid, int devicegroup) {
        Example example = new Example(Dlqjcsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        criteria.andEqualTo("inuse", 1);
        criteria.andEqualTo("cmd", 67);
        criteria.andEqualTo("lastcommunicationstatus", 0);
        return selectCountByExample(example);
    }

    /**
     * 获取通讯异常的（不判断报警不报警）设备总条数
     * 
     * @param stationid
     * @param devicegroup
     * @return
     */
    @Override
    public int getAbnormalCount(int stationid, int devicegroup) {
        Example example = new Example(Dlqjcsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        criteria.andEqualTo("inuse", 1);
        criteria.andEqualTo("cmd", 67);
        criteria.andEqualTo("lastcommunicationstatus", 1);
        int abcount1 = selectCountByExample(example);
        criteria.andEqualTo("lastcommunicationstatus", -1);
        int abcount2 = selectCountByExample(example);
        return abcount1 + abcount2;
    }

    /**
     * 更新运行状态
     * 
     * @param ad
     * @param addr
     * @param b
     */
    @Override
    public void UpdateStatus(Appdevice ad, Integer addr, int b) {
        Dlqjcsensor record = new Dlqjcsensor();
        record.setStationid(ad.getStationid());
        record.setDevicegroup(ad.getDevicegroup());
        record.setLastcommunicationstatus(b);
        record.setDeviceaddr(addr);
        Update(record, false);
    }
}
